home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / graphicgems4.lha / GemsIV / data_smooth / smooth1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  1.0 KB  |  32 lines

  1. /* Program 1. Smoothing and interpolation with first differences. */
  2. /* Contribution to Graphic Gems IV */
  3.  
  4. /* Paul H. C. Eilers, DCMR Milieudienst Rijnmond, 's-Gravelandseweg 565,
  5.    3119 XT Schiedam, The Netherlands, E-Mail: paul@dcmr.nl */
  6.  
  7. #define MMAX 100    /* choose the right length for your application */
  8.  
  9. typedef float vec[MMAX + 1];
  10.  
  11. void smooth1(vec w, vec y, vec z, float lambda, int m)
  12. /* Smoothing and interpolation with first differences.
  13.    Input:  weights (w), data (y): vector from 1 to m.
  14.    Input:  smoothing parameter (lambda), length (m).
  15.    Output: smoothed vector (z): vector from 1 to m. */
  16. {
  17.   int i, i1;
  18.   vec c, d;
  19.   d[1] = w[1] + lambda;
  20.   c[1] = -lambda / d[1];
  21.   z[1] = w[1] * y[1];
  22.   for (i = 2; i < m; i++) {
  23.     i1 = i - 1;
  24.     d[i]= w[i] + 2 * lambda - c[i1] * c[i1] * d[i1];
  25.     c[i] = -lambda / d[i];
  26.     z[i] = w[i] * y[i] - c[i1] * z[i1];
  27.   }
  28.   d[m] = w[m] + lambda - c[m - 1] * c[m - 1] * d[m - 1];
  29.   z[m] = (w[m] * y[m] - c[m - 1] * z[m - 1]) / d[m];
  30.   for (i = m - 1; 1 <= i; i--) z[i] = z[i] / d[i] - c[i] * z[i + 1];
  31. };
  32.